package com.pingan.stat.reposys.money24.service;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import com.pingan.common.core.property.Properties;
import com.pingan.common.util.ShellExecutor;
import com.pingan.stat.service.StatService;

@Component
public class Money24StatService implements StatService {

	@Properties(name = "reposys.loan.pages.output.path")
	private String outputPath;

	@Properties(name = "reposys.loan.pages.scp.shell")
	private String scpShell;

	@Resource
	private ShellExecutor shellExecutor;

	@Resource
	private LoanPagesStatService loanPagesStatService;

	@Resource
	private LoanPagesSEMStatService loanPagesSEMStatService;

	private static final Log log = LogFactory.getLog("hive");

	@Override
	public void dailyStat(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String yyyyMMdd = sdf.format(date);
		String output = outputPath + "/" + yyyyMMdd;

		log.info("----- clean up 24money output path");
		cleanUpPath(output);

		log.info("------ " + yyyyMMdd);
		loanPagesStatService.dailyStat(date);
		loanPagesSEMStatService.dailyStat(date);

		log.info("----------------->>>>>>>>>>>>>>>>>> scp 24money output ");
		String[] scpParams = new String[] { output };
		shellExecutor.excecute(scpShell, scpParams);
	}

	public void cleanUpPath(String output) {
		File file = new File(output);
		if (file.isDirectory()) {
			try {
				FileUtils.forceDelete(file);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		file.mkdirs();
	}

}
